package org.argetr.resim.proc.tools;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;

import org.argetr.resim.proc.*;
import org.argetr.resim.proc.data.*;
import org.argetr.resim.proc.filter.ConvolutionPR;
import org.argetr.resim.proc.input.ImageInputPR;
import org.argetr.resim.proc.input.SubImagePR;
import org.argetr.resim.proc.math.ArithmeticPR;
import org.argetr.resim.proc.math.ComplexMagnitudePR;
import org.argetr.resim.proc.math.DFTPR;
import org.argetr.resim.proc.math.IDFTPR;
import org.argetr.resim.proc.math.ImageArithmeticPR;
import org.argetr.resim.proc.math.LogPR;
import org.argetr.resim.proc.math.MathPR;
import org.argetr.resim.proc.math.PixelWiseMaxPR;
import org.argetr.resim.proc.math.PixelWiseMinPR;
import org.argetr.resim.proc.math.PowerPR;
import org.argetr.resim.proc.view.ImgViewerPR;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYBarDataset;



public class Test {

	/**
	 * @param args
	 */
	public static void printError(int retCode){
		if(retCode == ProcessorGraph.RET_OK){
			return;
		}
		switch (retCode) {
		case ProcessorGraph.RET_INVALID_CONNECTION:
			System.out.println("INVALID CONNECTION");
			break;
		case ProcessorGraph.RET_PROCESSOR_EXIST:
			System.out.println("PROCESSOR EXISTS");
			break;
		case ProcessorGraph.RET_PROCESSOR_NOT_EXIST:
			System.out.println("PROCESSOR NOT EXIST");
			break;
		default:
			break;
		}
	}
	public static void main(String[] args) {

	  int retCode;
		 DefaultXYDataset xySet = new DefaultXYDataset();
		 xySet.addSeries("HI", new double[][] {{0,1,2},{3,4,5}});
		 XYBarDataset series = new XYBarDataset(xySet, 0.1);
        

		 JFreeChart thechart = ChartFactory.createXYBarChart("Histogram Chart",
		 "Bins",
		 false, "Total Numbers",
		 series,
		 PlotOrientation.VERTICAL,
		 true,
		 false,
		 false); 
		 ChartPanel panel = new ChartPanel(thechart);
			JFrame frame = new JFrame();
			frame.setTitle("Histogram Viewer");
			Container contentPane = frame.getContentPane();
			contentPane.setLayout(new BorderLayout());
			frame.setContentPane(panel);
			frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			frame.setVisible(true); // show the frame.
      ImageInputPR prImgInput = new ImageInputPR();
	  prImgInput.setParameters("ex.JPG");
      retCode = ProcessorGraph.getInstance().addProcessor(prImgInput);
      printError(retCode);
      
	  ColorConvertPR prColorConvert = new ColorConvertPR();
	  prColorConvert.setParameters(ColorConvertPR.GRAY);
      retCode = ProcessorGraph.getInstance().addProcessor(prColorConvert);
      printError(retCode);
      
      ImgViewerPR prImgView1 = new ImgViewerPR();
      retCode = ProcessorGraph.getInstance().addProcessor(prImgView1);
      printError(retCode);
      
      ArithmeticPR prArith = new ArithmeticPR();
      prArith.setParameters(MathPR.SUB);
      retCode = ProcessorGraph.getInstance().addProcessor(prArith);
      printError(retCode);
      
      NegativePR prNeg = new NegativePR();
      retCode = ProcessorGraph.getInstance().addProcessor(prNeg);
      printError(retCode);
      
      ImgViewerPR prImgView2 = new ImgViewerPR();
      retCode = ProcessorGraph.getInstance().addProcessor(prImgView2);
      printError(retCode);
      
      
      retCode = ProcessorGraph.getInstance().newConnection(prImgInput, 0, prColorConvert, 0);
      printError(retCode);
      retCode = ProcessorGraph.getInstance().newConnection(prColorConvert, 0, prNeg, 0);
      printError(retCode);
      retCode = ProcessorGraph.getInstance().newConnection(prNeg, 0, prImgView1, 0);
      printError(retCode);
      retCode = ProcessorGraph.getInstance().newConnection(prColorConvert, 0, prArith, 0);
      printError(retCode);
      retCode = ProcessorGraph.getInstance().newConnection(prNeg, 0, prArith, 1);
      printError(retCode);
      retCode = ProcessorGraph.getInstance().newConnection(prArith, 0, prImgView2, 0);
      printError(retCode);
      
      ProcessorGraph.getInstance().run();
	  if(true){
		  return;
	  }
	  
      Processor pr0 = new ImageInputPR();
	  ((ImageInputPR)pr0).setParameters("ex.JPG");
	  Data o0 = pr0.process();

	  
	  //view(o0);
	  
      Processor pr1 = new ColorConvertPR();
      ((ColorConvertPR)pr1).setParameters(ColorConvertPR.GRAY);
      Data o1 = pr1.process(o0);
      //view(o1);
     
      
      float [][] kernel = {{-1, -2, -1},
    		              {0,  0,  0},
    		              {1,  2,  1}};
      KernelDA kernelData = new KernelDA(kernel);
      Processor pr2 = new ConvolutionPR();
      ((ConvolutionPR)pr2).setParameters(kernelData);
      Data o2 = pr2.process(o1);
      
      //view(o2);
      
      Processor pr3 = new NegativePR();
      Data o3 = pr3.process(o1);
     // view(o3);

      Processor pr4 = new LogPR();
      Data o4 = pr4.process(o1);
      
      Processor pr5 = new ReScalePR();
      Data o5 = pr5.process(o4);
      
     // view(o5);
      
      Processor pr6 = new HistogramPR();
      pr6.process(o1);

      Processor pr7 = new HistogramEqualizePR();
      ImageDA o7 = (ImageDA)pr7.process(o1);

      //view(o7);
 
      Processor pr8 = new ArithmeticPR();
      ((ArithmeticPR)pr8).setParamOpType(MathPR.SUB);
      ConstDA constDA = new ConstDA(255);
      pr8.setInput(constDA, 0);
      pr8.setInput(o1, 1);
      ImageDA o8 = (ImageDA)pr8.process();

      //view(o8);

      Processor pr9 = new ImageArithmeticPR();
      ((ImageArithmeticPR)pr9).setParameters(MathPR.ADD);
      pr9.setInput(o1, 0);
      pr9.setInput(o2, 1);
      ImageDA o9 = (ImageDA)pr9.process();
      //view(o9);
      
      Processor pr10 = new SubImagePR();
      ((SubImagePR)pr10).setParameters(0, 0, 100, 100);
      ImageDA o10 = (ImageDA) pr10.process(o9);
    
      Processor pr11 = new SubImagePR();
      ((SubImagePR)pr11).setParameters(0, 0, 100, 100);
      ImageDA o11 = (ImageDA) pr11.process(o1);
      //view(o11);
      
      Processor pr12 = new ImageArithmeticPR();
      ((ImageArithmeticPR)pr12).setParameters(MathPR.SUB);
      pr12.setInput(o10, 0);
      pr12.setInput(o11, 1);
      ImageDA o12 = (ImageDA)pr12.process();
      //view(o12);
      
      Processor pr13 = new DFTPR();
      ImageDA o13 = (ImageDA)pr13.process(o11);
      //view(o13);
      
      Processor pr14 = new ComplexMagnitudePR();
      ImageDA o14 = (ImageDA)pr14.process(o13);
      //view(o14);
      
      Processor pr15 = new LogPR();
      ImageDA o15 = (ImageDA)pr15.process(o14);
      //view(o15);
      Processor pr16 = new ReScalePR();
      ImageDA o16 = (ImageDA)pr16.process(o15);
     // view(o16);
      
      Processor pr17 = new IDFTPR();
      ImageDA o17 = (ImageDA)pr17.process(o13);
     //view(o17);
      
      Processor pr18 = new PowerPR();
      ((PowerPR)pr18).setParameters(2.0);
      ImageDA o18 = (ImageDA)pr18.process(o11);
      //view(o18);   
      
      Processor pr19 = new ReScalePR();
      ImageDA o19 = (ImageDA)pr19.process(o18);
      //view(o19);
      
      PixelWiseMaxPR pr20 = new PixelWiseMaxPR();
      pr20.setInput(o1, 0);
      pr20.setInput(o3, 1);
      Data o20 = pr20.process();
      //view(o20);
      
      PixelWiseMinPR pr21 = new PixelWiseMinPR();
      pr21.setInput(o1, 0);
      pr21.setInput(o3, 1);
      Data o21 = pr21.process();
      //view(o21);
      
      ThresholdPR pr22 = new ThresholdPR();
      pr22.setParameters(128, 0, 255);
      Data o22 = pr22.process(o1);
      //view(o22);
      
      BandSelectPR pr23 = new BandSelectPR();
      pr23.setParameters(2);
      Data o23 = pr23.process(o0);
      view(o23);
		
	}
    public static void view(Data d){
        Processor pr = new ImgViewerPR();
        pr.process(d);
    }
}
